package com.cssnj.portal.service.user;

import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.service.base.BaseService;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("explainService")
public class ExplainService extends BaseService {
    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        if ("init".equals(jsonRequest.getHandleCode())) {
            return init(jsonRequest);
        }
        return null;
    }

    //初始化
    private JsonResponse init(JsonRequest jsonRequest) throws SQLException {
        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map;
        List<Map<String, Object>> jsList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> xqList = new ArrayList<Map<String, Object>>();
        String gnmc = "";
        String gnid = (String) jsonRequest.getReqData().get("gnid");
        //查询基本信息
        String sql = "select a.gn_mc gnmc, b.gnms, b.xqsm, b.jssm, c.yy_mc yymc from gl_gn a, yy_gn_jr b, yy_jcxx c where a.gn_id = ? and a.gn_id = b.gn_id(+) and a.xt_dm = c.yy_id(+)";
        List<Object> params = new ArrayList<Object>();
        params.add(gnid);
        SqlRowSet rs = this.queryDao.queryForRowSet(sql, params);
        if (rs.next()) {
            jsonResponse.getRepData().put("gnmc", rs.getString("gnmc"));
            jsonResponse.getRepData().put("gnms", rs.getString("gnms"));
            jsonResponse.getRepData().put("xqsm", rs.getString("xqsm"));
            jsonResponse.getRepData().put("jssm", rs.getString("jssm"));
            jsonResponse.getRepData().put("yymc", rs.getString("yymc"));
        }
        //查询技术说明附件
        sql = "select a.fjmc,a.fjlj from yy_gn_jr_jssm a where a.gn_id = ?";
        rs = this.queryDao.queryForRowSet(sql, params);
        while (rs.next()) {
            map = new HashMap<String, Object>();
            map.put("mc", rs.getString("fjmc"));
            map.put("lj", rs.getString("fjlj"));
            jsList.add(map);
        }
        jsonResponse.getRepData().put("jsList", jsList);
        //查询需求说明附件
        sql = "select a.fjmc,a.fjlj from yy_gn_jr_xqsm a where a.gn_id = ?";
        rs = this.queryDao.queryForRowSet(sql, params);
        while (rs.next()) {
            map = new HashMap<String, Object>();
            map.put("mc", rs.getString("fjmc"));
            map.put("lj", rs.getString("fjlj"));
            xqList.add(map);
        }
        jsonResponse.getRepData().put("xqList", xqList);
        return jsonResponse;
    }
}
